package com.fz.song.service;


import com.fz.song.enums.ExceptionEnum;
import com.fz.song.enums.UserEnum;
import com.fz.song.exception.CustomException;
import com.fz.song.mapping.*;
import com.fz.song.pojo.*;
import com.fz.song.util.NumberUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    public SysRoleMapper sysRoleMapper;
    @Autowired
    public SysRoleResourceMapper sysRoleResourceMapper;
    @Autowired
    public SysResourceMapper sysResourceMapper;
    @Autowired
    public SysRoleUserMapper sysRoleUserMapper;

    public PageInfo<SysRole> queryRoleByPage(Map<String, Object> params, int pageNo, int pageSize) {
        pageNo = pageNo == 0 ? 1 : pageNo;
        pageSize = pageSize == 0 ? 10 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<SysRole> list = sysRoleMapper.getRoleList(params);
        PageInfo<SysRole> page = new PageInfo<SysRole>(list);
        return page;
    }

    public SysRole getRoleById(int roleid) throws CustomException {
        if (roleid < 1) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }
        return sysRoleMapper.selectByPrimaryKey(roleid);
    }

    public int saveRole(SysRole role) throws CustomException {
        if (role == null) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }
        Integer sysid = role.getSysid();
        int num = 0;
        if (sysid == null || sysid == 0) {
            //新增
            num = sysRoleMapper.insertSelective(role);
        } else {
            //修改
            num = sysRoleMapper.updateByPrimaryKeySelective(role);
        }
        return num;
    }

    public int deleteRole(int sysid) throws CustomException {
        if (sysid < 1) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }
        return sysRoleMapper.deleteByPrimaryKey(sysid);
    }

    public boolean roles_savePowers(Integer sysid, String powerss) throws CustomException {
        if (null == powerss || "".equals(powerss)) {
            return false;
        }

        //组装参数
        String[] split = powerss.split(",");
        List<SysRoleResource> list = new ArrayList<SysRoleResource>();

        //第一步,清除原来的
        sysRoleResourceMapper.cleanRole(sysid);
        //再添加新的权限
        for (int i = 0; i < split.length; i++) {
            SysRoleResource sysRoleResource = new SysRoleResource();
            sysRoleResource.setRoleid(sysid);
            sysRoleResource.setResourceid(NumberUtil.Str2Int(split[i], 0));
            list.add(sysRoleResource);
        }
        int insertbatch = sysRoleResourceMapper.insertbatch(list);
        if (insertbatch >= 0) {
            return true;
        }
        return false;
    }

    public List<SysRole> getRoleListAll() {

        return sysRoleMapper.getRoleListAll();
    }

    public List<SysRoleUser> getUserRole(int sysid) {
        return sysRoleUserMapper.getUserRoleByUserId(sysid);
    }

    public Boolean saveUserRole(int userid, String roleids) {
        if (null == roleids || "".equals(roleids)) {
            return false;
        }
        sysRoleUserMapper.cleanUserRole(userid);


        String[] split = roleids.split(",");

        List<SysRoleUser> list = new ArrayList<SysRoleUser>();

        for (String roleid : split) {
            SysRoleUser roleUser = new SysRoleUser();
            roleUser.setRoleid(NumberUtil.Str2Int(roleid, -1));
            roleUser.setUserid(userid);
            list.add(roleUser);
        }

        int num = sysRoleUserMapper.insertbatch(list);
        if (num >= 0)
            return true;
        return false;
    }

    public List<SysResource> getMyResource(int userid) {
        return sysResourceMapper.getMyResource(userid);
    }


}
